package com.vmladenov.cook.ui.recipes;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.vmladenov.cook.R;
import com.vmladenov.cook.core.GlobalStrings;
import com.vmladenov.cook.core.rss.RSSItem;
import com.vmladenov.cook.core.rss.RSSReader;
import com.vmladenov.cook.ui.SimpleLinkListActivity;

public class LatestRecipesActivity extends SimpleLinkListActivity<RSSItem>
{
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.rssmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case R.id.miRefresh:
				LoadData();
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onListItemClick(ListView parent, View v, int position, long id)
	{
		Intent intent = new Intent(LatestRecipesActivity.this, ViewRecipeActivity.class);
		ListAdapter adapter = getListAdapter();
		RSSItem item = (RSSItem) adapter.getItem(position);
		Bundle bundle = new Bundle();
		bundle.putString("url", item.getLink());
		intent.putExtra("RECIPE_URL", bundle);
		startActivity(intent);
	}

	@Override
	protected ArrayList<RSSItem> getData()
	{
		RSSReader reader = new RSSReader();
		return reader.getFeed(GlobalStrings.LatestRecipesFeed);
	}
}